package org.jeecg.modules.wms.inorder.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.wms.config.WarehouseDictEnum;
import org.jeecg.modules.wms.goods.service.IWmsProductsService;
import org.jeecg.modules.wms.inorder.entity.WmsStockInOrderItems;
import org.jeecg.modules.wms.inorder.mapper.WmsStockInOrderItemsMapper;
import org.jeecg.modules.wms.inorder.service.IWmsStockInOrderItemsService;
import org.jeecg.modules.wms.wmstask.entity.WmsTasks;
import org.jeecg.modules.wms.wmstask.entity.WmsTasksRecords;
import org.jeecg.modules.wms.wmstask.service.IWmsTasksRecordsService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @Description: 入库单明细
 * @Author: jeecg-boot
 * @Date: 2025-10-27
 * @Version: V1.0
 */
@Service
public class WmsStockInOrderItemsServiceImpl extends ServiceImpl<WmsStockInOrderItemsMapper, WmsStockInOrderItems> implements IWmsStockInOrderItemsService {

    @Autowired
    private WmsStockInOrderItemsMapper wmsStockInOrderItemsMapper;
    @Autowired
    private IWmsProductsService wmsProductsService;
    @Autowired
    private IWmsTasksRecordsService wmsTasksRecordsService;

    @Override
    public void updateStatusPutAway(String stockInOrderItemId) {
        WmsStockInOrderItems wmsStockInOrderItem = this.getById(stockInOrderItemId);
        //根据入库单明细id查询任务记录表，且任务类型为上架任务
        List<WmsTasksRecords> wmsTasksRecords = wmsTasksRecordsService.list(Wrappers.lambdaQuery(WmsTasksRecords.class)
                .eq(WmsTasksRecords::getStockInOrderItemId, stockInOrderItemId)
                .eq(WmsTasksRecords::getTaskType, WarehouseDictEnum.TASK_TYPE_PUTAWAY.getCode())
        );
        //统计每次执行的数量，并更新入库单明细表中的上架数量
        int goodQuantity = wmsTasksRecords.stream().mapToInt(WmsTasksRecords::getExecQuantity).sum();
        boolean update = this.update(wmsStockInOrderItem, Wrappers.lambdaUpdate(WmsStockInOrderItems.class)
                .set(WmsStockInOrderItems::getShelvedQuantity, goodQuantity)
                .eq(WmsStockInOrderItems::getId, stockInOrderItemId)
        );
        if (!update) {
            throw new JeecgBootException("更新入库单明细上架数量失败");
        }

        //得到入库单明细表中的实际收货数量，与更新后的上架数量进行对比，如果相等则更新入库单明细表状态为完成INBOUND_DETAIL_PUTAWAYED.getCode()
        wmsStockInOrderItem = this.getById(stockInOrderItemId);
        if (Objects.equals(wmsStockInOrderItem.getShelvedQuantity(), wmsStockInOrderItem.getReceivedQuantity())) {
            boolean updated = this.update(Wrappers.lambdaUpdate(WmsStockInOrderItems.class)
                    .set(WmsStockInOrderItems::getStatus, WarehouseDictEnum.INBOUND_DETAIL_PUTAWAYED.getCode())
                    .eq(WmsStockInOrderItems::getId, stockInOrderItemId)
            );
            if (!updated) {
                throw new JeecgBootException("更新入库单明细状态失败");
            }
        }
    }


    @Override
    public void updateStatus(String stockInOrderItemId) {
        //统计良品和不良品的数量-》任务记录表统计
        //根据入库单明细id查询任务记录表，类型是收货
        List<WmsTasksRecords> wmsTasksRecords = wmsTasksRecordsService.list(Wrappers.lambdaQuery(WmsTasksRecords.class)
                .eq(WmsTasksRecords::getStockInOrderItemId, stockInOrderItemId)
                .eq(WmsTasksRecords::getTaskType, WarehouseDictEnum.TASK_TYPE_RECEIVING.getCode())
        );
        //统计良品和不良品的数量
        int goodQuantity = wmsTasksRecords.stream()
                .filter(wmsTasksRecord -> WarehouseDictEnum.INVENTORY_ATTRIBUTE_GOOD.getCode().equals(wmsTasksRecord.getInventoryAttribute()))
                .mapToInt(WmsTasksRecords::getExecQuantity).sum();
        int badQuantity = wmsTasksRecords.stream()
                .filter(wmsTasksRecord -> WarehouseDictEnum.INVENTORY_ATTRIBUTE_DEFECTIVE.getCode().equals(wmsTasksRecord.getInventoryAttribute()))
                .mapToInt(WmsTasksRecords::getExecQuantity).sum();

        //获取入库单明细的预期收货数量
        WmsStockInOrderItems wmsStockInOrderItem = this.getById(stockInOrderItemId);
        Integer expectedQuantity = wmsStockInOrderItem.getExpectedQuantity();

        //良品+不良品数量=收货数量，状态为完成
        boolean update = this.update(Wrappers.lambdaUpdate(WmsStockInOrderItems.class)
                .set(goodQuantity > 0, WmsStockInOrderItems::getReceivedQuantity, goodQuantity)
                .set(badQuantity > 0, WmsStockInOrderItems::getDefectiveQuantity, badQuantity)
                .set(goodQuantity + badQuantity == expectedQuantity, WmsStockInOrderItems::getStatus, WarehouseDictEnum.INBOUND_DETAIL_RECEIVED.getCode())
                .eq(WmsStockInOrderItems::getId, stockInOrderItemId)
        );
        if (!update) {
            throw new JeecgBootException("更新入库单明细状态失败");
        }
    }


    @Override
    public List<WmsStockInOrderItems> selectByMainId(String mainId) {
        List<WmsStockInOrderItems> wmsStockInOrderItems = wmsStockInOrderItemsMapper.selectByMainId(mainId);
        //商品名称
        for (WmsStockInOrderItems wmsStockInOrderItem : wmsStockInOrderItems) {
            wmsStockInOrderItem.setProductName(wmsProductsService.getById(wmsStockInOrderItem.getProductId()).getProductName());
        }
        return wmsStockInOrderItems;
    }
}
